
#include <assert.h>
#include <stdint.h>
#include <stdlib.h>
#include <sys/time.h>
#include <stdio.h>
#include <NDL.h>

int min(int a, int b) {
    if (a > b) return b;
    return a;
}

int main() {
    FILE* fp = fopen("/share/music/test.pcm", "r");
    assert(fp);
    fseek(fp, 0, SEEK_END);
    size_t audio_len = ftell(fp);
    void* buf = malloc(audio_len);
    assert(audio_len);
    fseek(fp, 0, SEEK_SET);
    int ret = fread(buf, audio_len, 1, fp);
    assert(ret == 1);
    fclose(fp);
    NDL_Init(0);
    NDL_OpenAudio(8000, 1, 1024);

    int nplay = 0;
    uint8_t* start = buf;
    while (nplay < audio_len) {
        int len = (audio_len - nplay > 4096) ? 4096 : audio_len - nplay;
        NDL_PlayAudio(start, len);
        start += len;
        nplay += len;
        printf("Already play %d/%ld bytes of data\n", nplay, audio_len);
    }

    free(buf);
    return 0;
}
